package com.zmj.framework.handler.exception;

import com.zmj.framework.exception.remote.RemoteAuthorityException;
import com.zmj.framework.exception.remote.RemoteException;
import com.zmj.framework.exception.remote.RemoteServiceException;
import com.zmj.framework.exception.remote.RemoteTimeoutException;
import com.zmj.framework.response.ResponseCode;
import com.zmj.framework.response.Result;
import feign.FeignException;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class RemoteExceptionControllerAdvice {

    @ExceptionHandler(RemoteAuthorityException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleRemoteAuthorityException(RemoteAuthorityException ex) {
        return ExceptionMapperUtil.buildBody(ex, ResponseCode.REMOTE_AUTHORITY_ERROR);
    }

    @ExceptionHandler(RemoteServiceException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleRemoteServiceException(RemoteServiceException ex) {
        return ExceptionMapperUtil.buildBody(ex, ResponseCode.REMOTE_SERVICE_ERROR);
    }

    @ExceptionHandler(RemoteTimeoutException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleRemoteTimeoutException(RemoteTimeoutException ex) {
        return ExceptionMapperUtil.buildBody(ex, ResponseCode.REMOTE_TIME_OUT);
    }

    @ExceptionHandler(RemoteException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleRemoteException(RemoteException ex) {
        return ExceptionMapperUtil.buildBody(ex, ResponseCode.REMOTE_ERROR);
    }

    @ExceptionHandler(FeignException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleFeignException(FeignException ex) {
        return ExceptionMapperUtil.buildBody(ex, ResponseCode.REMOTE_ERROR);
    }

}
